package com.example.asus.simpleplay.MyService;

/**首先导入所需的包*/
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;

import com.example.asus.simpleplay.R;

public class MusicService extends Service {
    /**声明一个MediaPlayer对象 player*/
    private MediaPlayer mPlayer;

    @Override
    public void onCreate() {
        mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.gequ);
        //设置可以重复播放
        mPlayer.setLooping(true);
        super.onCreate();
    }

    /**在Onstart方法中播放指定的mp3文件*/
    @Override
    public void onStart(Intent intent, int startId) {
        Toast.makeText(MusicService.this, "MusicSevice onStart()"
                , Toast.LENGTH_SHORT).show();
        mPlayer.start();
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        Toast.makeText(MusicService.this, "MusicSevice onDestroy()"
                , Toast.LENGTH_SHORT).show();
        mPlayer.stop();
        super.onDestroy();
    }

    /** onBind() 方法且返回一个 IBinder 对象，实现其他组件可以通过这个 IBinder 对象与该 service 进行通讯*/
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
